package com.airticketbooking.service.serviceImpl;

import com.airticketbooking.common.constant.StatusConstant;
import com.airticketbooking.common.domain.PageQuery;
import com.airticketbooking.common.utils.Md5Util;
import com.airticketbooking.common.utils.UserContext;
import com.airticketbooking.po.Employee;
import com.airticketbooking.mapper.EmployeeMapper;
import com.airticketbooking.po.User;
import com.airticketbooking.service.EmployeeService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author Mr.J
 * @version 1.0.0
 * @date 2024/11/10
 * @desc
 */

@Service
@RequiredArgsConstructor
public class EmployeeServiceImpl implements EmployeeService {

	private final EmployeeMapper employeeMapper;



	@Override
	public Employee getByEmployeeName(String employeeName) {

		Employee employee = employeeMapper.getByEmployeeName(employeeName);

		return employee;
	}


	@Override
	public void modify(Employee addemployee) {


		Employee employeeById = employeeMapper.getById(UserContext.getUser().intValue());

		if (employeeById == null) {
			throw new IllegalArgumentException("用户不存在");
		}

//		判断邮箱和手机号是否已被注册

		List<Employee> employeesAll = employeeMapper.getAll();

//		循环遍历所有用户，判断邮箱和手机号是否已被注册
		for (Employee employee : employeesAll) {
			if (employee.getEmail() != null && employee.getEmail().equals(addemployee.getEmail()) && employee.getEmployeeId() != employeeById.getEmployeeId()) {
				throw new IllegalArgumentException("邮箱已被他人注册");
			}
			if (employee.getPhoneNumber() != null && employee.getPhoneNumber().equals(addemployee.getPhoneNumber()) && employee.getEmployeeId() != employeeById.getEmployeeId()) {
				throw new IllegalArgumentException("手机号已被他人注册");
			}
		}


		employeeById.setEmail(addemployee.getEmail());
		employeeById.setPhoneNumber(addemployee.getPhoneNumber());


		employeeMapper.modify(employeeById);

	}

	@Override
	public Employee getById(Integer employeeId) {

		return employeeMapper.getById(employeeId);
	}

	@Override
	public Page<Employee> getempPage(PageQuery pageQuery) {
		PageHelper.startPage(pageQuery.getPageNo(), pageQuery.getPageSize());

		return employeeMapper.getempPage(pageQuery);
	}

	@Override
	public Employee getEmployeeInfo() {

		Long empId = UserContext.getUser();

		if (empId == null) {
			throw new RuntimeException("未登录");
		}

		return employeeMapper.getById(empId.intValue());

	}

	@Override
	public void register(String employeeName, String password, String rePassword) {


		Employee byEmployeeName = employeeMapper.getByEmployeeName(employeeName);

		if (byEmployeeName != null) {
			throw new RuntimeException("用户名已存在");
		}

		if (!password.equals(rePassword)) {
			throw new RuntimeException("两次密码输入不一致");
		}

		String md5Password = Md5Util.getMD5String(password);

		Employee employee = new Employee();
		employee.setEmployeeName(employeeName);
		employee.setPassword(md5Password);
		employee.setHireDate(LocalDate.now());
		employee.setStatus(StatusConstant.ENABLE);

		employeeMapper.add(employee);


	}

	@Override
	public void changePassword(String oldPassword, String newPassword, String rePassword) {

//		判断旧密码是否正确
		Employee employeeById = employeeMapper.getById(UserContext.getUser().intValue());

		if (employeeById == null) {
			throw new IllegalArgumentException("用户不存在");
		}

		if (!employeeById.getPassword().equals(Md5Util.getMD5String(oldPassword))) {
			throw new IllegalArgumentException("旧密码错误");
		}

		if (!newPassword.equals(rePassword)) {
			throw new IllegalArgumentException("两次密码输入不一致");
		}

		employeeById.setPassword(Md5Util.getMD5String(newPassword));

		employeeMapper.modify(employeeById);


	}

	@Override
	public void changeAvatar(String avatarUrl) {


		Integer empId = UserContext.getUser().intValue();


		Employee employee = new Employee();
		employee.setEmployeeId(empId);
		employee.setEmpPic(avatarUrl);

		employeeMapper.modify(employee);


	}
}
